/* add user code begin Header */
/**
 **************************************************************************
 * @file     main.c
 * @brief    main program
 **************************************************************************
 *                       Copyright notice & Disclaimer
 *
 * The software Board Support Package (BSP) that is made available to
 * download from Artery official website is the copyrighted work of Artery.
 * Artery authorizes customers to use, copy, and distribute the BSP
 * software and its related documentation for the purpose of design and
 * development in conjunction with Artery microcontrollers. Use of the
 * software is governed by this copyright notice and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
 * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
 * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
 * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
 * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 **************************************************************************
 */
/* add user code end Header */

/* Includes ------------------------------------------------------------------*/
#include "at32f421_wk_config.h"
#include "wk_system.h"

/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "string.h"
#include "stdio.h"

/* add user code end private includes */

/* private typedef -----------------------------------------------------------*/
/* add user code begin private typedef */

/* add user code end private typedef */

/* private define ------------------------------------------------------------*/
/* add user code begin private define */

/* add user code end private define */

/* private macro -------------------------------------------------------------*/
/* add user code begin private macro */

/* add user code end private macro */

/* private variables ---------------------------------------------------------*/
/* add user code begin private variables */

volatile uint8_t usart1_tx_dma_status = 0;
volatile uint8_t usart1_rx_dma_status = 0;

volatile uint8_t usart1_rx_buffer[DMA1_CHANNEL3_BUFFER_SIZE];
volatile uint16_t usart1_rx_buffer_size = 0;

volatile uint16_t adc1_ordinary_valuetab[DMA1_CHANNEL1_BUFFER_SIZE];

/* add user code end private variables */

/* private function prototypes --------------------------------------------*/
/* add user code begin function prototypes */
uint8_t key_flag = 0;
uint8_t Drive_temp_bright[] = {0x01, 0xA0, 0x04, 0x03, 0xE8, 0x01, 0x03, 0x94};
/* add user code end function prototypes */

/* private user code ---------------------------------------------------------*/
/* add user code begin 0 */

int fputc(int ch, FILE *p)
{
  usart_data_transmit(USART1, (uint8_t)ch);
  while (usart_flag_get(USART1, USART_TDC_FLAG) == RESET)
    ;
  ch = usart_data_receive(USART1);
  return ch;
}

void usart_send(usart_type *usart_x, uint8_t *data, uint16_t len)
{
  while (len)
  {
    while (usart_flag_get(USART1, USART_TDBE_FLAG) == RESET)
      ;
    usart_x->dt = (*data & 0x01FF);
    len--;
    data++;
  }
}
void usart1_dma_send(uint8_t *data, uint16_t len)
{
  dma_channel_enable(DMA1_CHANNEL2, FALSE);
  wk_dma_channel_config(DMA1_CHANNEL2,
                        (uint32_t)&USART1->dt,
                        (uint32_t)data,
                        len); // 修改为实际字符串长度
  dma_channel_enable(DMA1_CHANNEL2, TRUE);
  while (dma_flag_get(DMA1_FDT2_FLAG) == SET)
    ;
  while (usart_flag_get(USART1, USART_TDC_FLAG) == RESET)
    ; // 等待发送完成
}

/* add user code end 0 */

/**
  * @brief main function.
  * @param  none
  * @retval none
  */
int main(void)
{
  /* add user code begin 1 */

  /* add user code end 1 */

  /* system clock config. */
  wk_system_clock_config();

  /* config periph clock. */
  wk_periph_clock_config();

  /* nvic config. */
  wk_nvic_config();

  /* timebase config. */
  wk_timebase_init();

  /* init dma1 channel1 */
  wk_dma1_channel1_init();
  /* config dma channel transfer parameter */
  /* user need to modify define values DMAx_CHANNELy_XXX_BASE_ADDR and DMAx_CHANNELy_BUFFER_SIZE in at32xxx_wk_config.h */
  wk_dma_channel_config(DMA1_CHANNEL1, 
                        (uint32_t)&ADC1->odt, 
                        DMA1_CHANNEL1_MEMORY_BASE_ADDR, 
                        DMA1_CHANNEL1_BUFFER_SIZE);
  dma_channel_enable(DMA1_CHANNEL1, TRUE);

  /* init dma1 channel2 */
  wk_dma1_channel2_init();
  /* config dma channel transfer parameter */
  /* user need to modify define values DMAx_CHANNELy_XXX_BASE_ADDR and DMAx_CHANNELy_BUFFER_SIZE in at32xxx_wk_config.h */
  wk_dma_channel_config(DMA1_CHANNEL2, 
                        (uint32_t)&USART1->dt, 
                        DMA1_CHANNEL2_MEMORY_BASE_ADDR, 
                        DMA1_CHANNEL2_BUFFER_SIZE);
  dma_channel_enable(DMA1_CHANNEL2, TRUE);

  /* init dma1 channel3 */
  wk_dma1_channel3_init();
  /* config dma channel transfer parameter */
  /* user need to modify define values DMAx_CHANNELy_XXX_BASE_ADDR and DMAx_CHANNELy_BUFFER_SIZE in at32xxx_wk_config.h */
  wk_dma_channel_config(DMA1_CHANNEL3, 
                        (uint32_t)&USART1->dt, 
                        DMA1_CHANNEL3_MEMORY_BASE_ADDR, 
                        DMA1_CHANNEL3_BUFFER_SIZE);
  dma_channel_enable(DMA1_CHANNEL3, TRUE);

  /* init usart1 function. */
  wk_usart1_init();

  /* init adc1 function. */
  wk_adc1_init();

  /* init exint function. */
  wk_exint_config();

  /* init gpio function. */
  wk_gpio_config();

  /* init tmr17 function. */
  wk_tmr17_init();

  /* add user code begin 2 */

  /* enable transfer full data interrupt */
  dma_interrupt_enable(DMA1_CHANNEL2, DMA_FDT_INT, TRUE);

  /* dma1 channel2 interrupt nvic init */
  nvic_irq_enable(DMA1_Channel3_2_IRQn, 0, 0);

  /* enable transfer full data interrupt */
  dma_interrupt_enable(DMA1_CHANNEL3, DMA_FDT_INT, TRUE);

  /* dma1 channel3 interrupt nvic init */
  nvic_irq_enable(DMA1_Channel3_2_IRQn, 0, 0);

  usart_interrupt_enable(USART1, USART_IDLE_INT, TRUE);
  nvic_irq_enable(USART1_IRQn, 0, 0);
  usart_enable(USART1, TRUE);
  adc_ordinary_software_trigger_enable(ADC1, TRUE); // 启用 ADC1 的软件触发

  tmr_interrupt_enable(TMR17, TMR_OVF_INT, TRUE);

  /* add user code end 2 */

  while(1)
  {
    /* add user code begin 3 */
    usart1_dma_send(Drive_temp_bright, sizeof(Drive_temp_bright));
    wk_delay_ms(50);
    // usart_send(USART1,usart1_tx_buffer,strlen(usart1_tx_buffer));
    //dma_channel_enable(DMA1_CHANNEL1, FALSE);
    wk_delay_ms(50);
    if (usart1_rx_dma_status == 1)
    {
      usart1_rx_dma_status = 0;
      usart1_dma_send((uint8_t *)usart1_rx_buffer, usart1_rx_buffer_size);
    }
    

    dma_channel_enable(DMA1_CHANNEL1, TRUE);
    //usart1_dma_send((uint8_t *)adc1_ordinary_valuetab, DMA1_CHANNEL1_BUFFER_SIZE);
    //printf("ADC通道1获取的值:%d\r\n", adc1_ordinary_valuetab[0]);
    Drive_temp_bright[3] = (uint8_t)(adc1_ordinary_valuetab[0] >> 8 & 0xff);  
    Drive_temp_bright[4] = (uint8_t)(adc1_ordinary_valuetab[0] & 0xff);

    //tmr_counter_value_set(TMR17, 0);
    //tmr_counter_enable(TMR17, FALSE);
    if(key_flag == 1)
    {
      key_flag = 0;
      GPIOF->odt ^= LED3_PIN;
      GPIOF->odt ^= LED2_PIN; 
      GPIOB->odt ^= LED4_PIN; 
    }

    /* add user code end 3 */
  }
}
